﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ProjectLoggingBusiness : BaseBusiness<PB_ProjectLogging>, IPB_ProjectLoggingBusiness, ITransientDependency
    {

        readonly IOperator _operator;
        public PB_ProjectLoggingBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_ProjectLogging>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ProjectLogging>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ProjectLogging, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ProjectLogging> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ProjectLogging data)
        {
            data.CreateTime = DateTime.Now;
            data.Deleted = false;
            data.CreatorId = _operator.UserId;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_ProjectLogging data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<PageResult<PB_ProjectLogging>> GetDataLogListAsync(PageInput<ConditionDTO> input, string Bulid)
        {
            List<PB_ProjectStatuLog> aa = await Db.GetIQueryable<PB_ProjectStatuLog>().Where(m=>m.BuildingnumberId==Bulid).ToListAsync();
            List<string> ParenmtId = new List<String>();
            if (aa.Count>0) {
                foreach (var item in aa) {
                    var bb =await Db.GetIQueryable<PB_ProjectLogging>().Where(m=>m.ParentId==item.Id).FirstOrDefaultAsync();
                    if (bb!=null) {
                        ParenmtId.Add(bb.ParentId);
                        
                    }
                }
            }
            var q = GetIQueryable().Where(m =>ParenmtId.Contains(m.ParentId)&&m.CreatorId==_operator.UserId); ;
            var where = LinqHelper.True<PB_ProjectLogging>();
            var search = input.Search;
            return await q.Where(where).GetPageResultAsync(input);
        }

        /// <summary>
        /// 获取协作者列表
        /// </summary>
        /// <param name="input"></param>
        /// <param name="Bulid"></param>
        /// <returns></returns>
        public async Task<PageResult<PB_ProjectLoggingDto>> GetDataLogDtoListAsync(PageInput<ConditionDTO> input)
        {
            Expression<Func<PB_ProjectLogging,PB_Buildingnumber,Base_User, PB_ProjectLoggingDto>> select = (a,c,d) => new PB_ProjectLoggingDto
            {
                BulNo=c.Buildingnumber,
                CreateIds=d.Id,
                Bulid=c.Id,
                DingUserId=d.DingUserId, 
            };
            var search = input.Search;
            select = select.BuildExtendSelectExpre();   
            var q = from a in GetIQueryable().AsExpandable()
                  
                    join c in Db.GetIQueryable<PB_Buildingnumber>() on a.BuildingNumberID equals c.Id into bc
                    from c in bc.DefaultIfEmpty()
                    join d in Db.GetIQueryable<Base_User>() on a.CreatorId equals d.Id into ad
                    from d in ad.DefaultIfEmpty()
                    select @select.Invoke(a, c,d);
            q = q.Where(m=>m.BuildingNumberID==search.Keyword&&m.DingUserId == search.Condition);
            var list = await q.GetPageResultAsync(input);
            return list;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}